package com.conversion.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by ${Spring} on 2017/7/17.
 */
public class DateUtil{

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    private static String YYYY_MM_DD ="yyyy-MM-dd";
    private static SimpleDateFormat simpleDateFormat;
    private static SimpleDateFormat sdf;
    private static DateFormat df;

    static {
        simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        sdf = new SimpleDateFormat(YYYY_MM_DD);
        df = DateFormat.getDateInstance();
    }


    public static Date getCurMonthFirstDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);//将小时至0
        calendar.set(Calendar.MINUTE, 0);//将分钟至0
        calendar.set(Calendar.SECOND, 0);//将秒至0
        calendar.set(Calendar.MILLISECOND, 0);//将毫秒至0
        Date date = calendar.getTime();//获得当前月第一天

        return date;
    }

    public static Date getCurMonthLastDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.MILLISECOND, -1);
        Date date = calendar.getTime();

        return date;
    }

    /**
     * 得到本周周一
     * @return
     */
    public static String getNowWeekBegin() {
        int mondayPlus;
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            mondayPlus = 0;
        } else {
            mondayPlus = 1 - dayOfWeek;
        }
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        return  df.format(monday);
    }

    /**
     * 得到本周周日
     * @return
     */
    public static String getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return df.format(c.getTime());
    }

    /**
     * 得到本月第一天
     * @return
     */
    public static String getFirstDayOfThisMonth(){
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return df.format(c.getTime());
    }

    /**
     * 得到本月最后一天
     * @return
     */
    public static String getLastDayOfThisMonth(){
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return df.format(ca.getTime());
    }

    /**
     * 得到两个日期之间共多少天
     * @param stateTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static int getDatePoor(String stateTime,String endTime) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long from = df.parse(stateTime).getTime();
        long to = df.parse(endTime).getTime();
        return (int) ((to - from) / (1000 * 60 * 60 * 24)+1);
    }

    /**
     * 增加天数
     * @param time
     * @param number
     * @return
     * @throws ParseException
     */
    public static String addDay(String time,int number) throws ParseException {
        Date date = df.parse(time);
        Calendar   calendar   =   new   GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,number);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往后推一天的结果
        return sdf.format(date);
    }

    /**
     * 增加天数,返回日期
     * @param date
     * @param number
     * @return
     * @throws ParseException
     */
    public static Date addDayDate(Date date,int number) throws ParseException {
        Calendar   calendar   =   new   GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,number);//把日期往后增加一天.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往后推一天的结果
        return date;
    }

    /** * 获取指定日期是星期几
     * @param time
     * @return
     */
    public static String getWeekOfDate(String time) throws ParseException {
        Date date = df.parse(time);
        String[] weekOfDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0){
            w = 0;
        }
        return weekOfDays[w];
    }

    /**
     * 得到当天七日
     * @return yyyy-MM-dd
     */
    public static  String getToday(){
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     *将时间戳转换为时间
     * @param s 时间戳
     * @return 返回时间格式 "1970-01-18 08:12:07"
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     *将时间转换为时间戳
     * @param s 时间格式 "1970-01-18 08:12:07"
     * @return 时间戳
     * @throws ParseException
     */
    public static String dateToStamp(String s) throws ParseException{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(getCurMonthFirstDay());//获取本月第一天日期
        System.out.println(getCurMonthLastDay());//获取本月最后一天日期
        System.out.println(getNowWeekBegin());//获取本周第一天日期
        System.out.println(getSundayOfThisWeek());//获取本周最后一天日期
        System.out.println(getFirstDayOfThisMonth());//获取这个月第一天日期
        System.out.println(getLastDayOfThisMonth());//获取这个月最后一天日期
        System.out.println(getWeekOfDate("2017-01-04"));//获取这个日期在周几
        System.out.println(getToday());//获取当前日期
        System.out.println(addDay("2017-07-17 14:22:00",-10)); //在某个日期的基础上增加或减少N天  返回格式:    2017-07-07
        System.out.println(addDayDate(new Date(),10));//在某个日期的基础上增加或减少N天  放回格式:     Thu Jul 27 16:58:10 CST 2017
        System.out.println(stampToDate("1500272520000"));//把时间戳转换为日期格式String 类型
        System.out.println(dateToStamp("2017-07-17 14:22:00.518"));//把String时间格式装好为时间戳
        System.out.println(getDatePoor("2017-04-20","2017-04-28")*(-1));//获取两个时间之间相差几天
    }


}
